package jinyilw.tools.net.dev;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class CommandPacket extends NetworkPacket
{
	private static final long serialVersionUID = 7166467722795508186L;
	public static final short ID = -254;

	protected short commandID = Short.MIN_VALUE;
	protected boolean simple = false;

	private int intData;
	private long longData;
	private float floatData;
	private double doubleData;
	private boolean boolData;
	private byte byteData;
	private String stringData = "";

	private int[] intArr;
	private long[] longArr;
	private float[] floatArr;
	private double[] doubleArr;
	private boolean[] boolArr;
	private byte[] byteArr;
	private String[] stringArr;

	public CommandPacket()
	{
	}

	public CommandPacket(short commandID)
	{
		this.commandID = commandID;
	}

	public CommandPacket(boolean simple)
	{
		this.simple = simple;
	}

	public CommandPacket(short commandID, boolean simple)
	{
		this.commandID = commandID;
		this.simple = simple;
	}

	@Override
	public void read(DataInputStream input) throws IOException
	{
		commandID = input.readShort();
		simple = input.readBoolean();
		if (simple)
			return;
		intData = input.readInt();
		longData = input.readLong();
		floatData = input.readFloat();
		doubleData = input.readDouble();
		boolData = input.readBoolean();
		byteData = input.readByte();
		stringData = input.readUTF();
		short length = input.readShort();
		if (length > 0)
		{
			intArr = new int[length];
			for (int i = 0; i < length; i++)
				intArr[i] = input.readInt();
		}
		length = input.readShort();
		if (length > 0)
		{
			longArr = new long[length];
			for (int i = 0; i < length; i++)
				longArr[i] = input.readLong();
		}
		length = input.readShort();
		if (length > 0)
		{
			floatArr = new float[length];
			for (int i = 0; i < length; i++)
				floatArr[i] = input.readFloat();
		}
		length = input.readShort();
		if (length > 0)
		{
			doubleArr = new double[length];
			for (int i = 0; i < length; i++)
				doubleArr[i] = input.readDouble();
		}
		length = input.readShort();
		if (length > 0)
		{
			boolArr = new boolean[length];
			for (int i = 0; i < length; i++)
				boolArr[i] = input.readBoolean();
		}
		length = input.readShort();
		if (length > 0)
		{
			byteArr = new byte[length];
			for (int i = 0; i < length; i++)
				byteArr[i] = input.readByte();
		}
		length = input.readShort();
		if (length > 0)
		{
			stringArr = new String[length];
			for (int i = 0; i < length; i++)
				stringArr[i] = input.readUTF();
		}
	}

	@Override
	public void write(DataOutputStream output) throws IOException
	{
		output.writeShort(commandID);
		output.writeBoolean(simple);
		if (simple)
			return;
		output.writeInt(intData);
		output.writeLong(longData);
		output.writeFloat(floatData);
		output.writeDouble(doubleData);
		output.writeBoolean(boolData);
		output.writeByte(byteData);
		output.writeUTF(stringData);
		short length = (short) (intArr != null ? intArr.length : 0);
		output.writeShort(length);
		for (int i = 0; i < length; i++)
			output.writeInt(intArr[i]);
		length = (short) (longArr != null ? longArr.length : 0);
		output.writeShort(length);
		for (int i = 0; i < length; i++)
			output.writeLong(longArr[i]);
		length = (short) (floatArr != null ? floatArr.length : 0);
		output.writeShort(length);
		for (int i = 0; i < length; i++)
			output.writeFloat(floatArr[i]);
		length = (short) (doubleArr != null ? doubleArr.length : 0);
		output.writeShort(length);
		for (int i = 0; i < length; i++)
			output.writeDouble(doubleArr[i]);
		length = (short) (boolArr != null ? boolArr.length : 0);
		output.writeShort(length);
		for (int i = 0; i < length; i++)
			output.writeBoolean(boolArr[i]);
		length = (short) (byteArr != null ? byteArr.length : 0);
		output.writeShort(length);
		for (int i = 0; i < length; i++)
			output.writeByte(byteArr[i]);
		length = (short) (stringArr != null ? stringArr.length : 0);
		output.writeShort(length);
		for (int i = 0; i < length; i++)
			output.writeUTF(stringArr[i]);
	}

	public void setCommandID(short commandID)
	{
		this.commandID = commandID;
	}

	public void setIntData(int intData)
	{
		this.intData = intData;
	}

	public void setLongData(long longData)
	{
		this.longData = longData;
	}

	public void setFloatData(float floatData)
	{
		this.floatData = floatData;
	}

	public void setDoubleData(double doubleData)
	{
		this.doubleData = doubleData;
	}

	public void setBoolData(boolean boolData)
	{
		this.boolData = boolData;
	}

	public void setByteData(byte byteData)
	{
		this.byteData = byteData;
	}

	public void setStringData(String stringData)
	{
		this.stringData = stringData;
	}

	public void setIntArr(int[] intArr)
	{
		this.intArr = intArr;
	}

	public void setLongArr(long[] longArr)
	{
		this.longArr = longArr;
	}

	public void setFloatArr(float[] floatArr)
	{
		this.floatArr = floatArr;
	}

	public void setDoubleArr(double[] doubleArr)
	{
		this.doubleArr = doubleArr;
	}

	public void setBoolArr(boolean[] boolArr)
	{
		this.boolArr = boolArr;
	}

	public void setByteArr(byte[] byteArr)
	{
		this.byteArr = byteArr;
	}

	public void setStringArr(String[] stringArr)
	{
		this.stringArr = stringArr;
	}

	public short getCommandID()
	{
		return commandID;
	}

	public int getIntData()
	{
		return intData;
	}

	public long getLongData()
	{
		return longData;
	}

	public float getFloatData()
	{
		return floatData;
	}

	public double getDoubleData()
	{
		return doubleData;
	}

	public boolean getBoolData()
	{
		return boolData;
	}

	public byte getByteData()
	{
		return byteData;
	}

	public String getStringData()
	{
		return stringData;
	}

	public int[] getIntArr()
	{
		return intArr;
	}

	public long[] getLongArr()
	{
		return longArr;
	}

	public float[] getFloatArr()
	{
		return floatArr;
	}

	public double[] getDoubleArr()
	{
		return doubleArr;
	}

	public boolean[] getBoolArr()
	{
		return boolArr;
	}

	public byte[] getByteArr()
	{
		return byteArr;
	}

	public String[] getStringArr()
	{
		return stringArr;
	}

	@Override
	public short getID()
	{
		return CommandPacket.ID;
	}

	@Override
	public void setId(short id)
	{
		throw new UnsupportedOperationException(
				"To change NetworkMessage ID, use NetworkMessage.ID = new_id instead. "
						+ "Or use NetworkObject to send String message with unique ID.");
	}

	@Override
	public String toString()
	{
		if (NetworkConfig.DEBUG)
			return super.toString();
		if (stringData != null)
			return stringData;
		return "CommandPacket: ";
	}
}
